package com.jianggy.observer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public  class ConcreteSubject implements Subject {
    List<Observer> observers = new ArrayList<>();

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Iterator<Observer> iterator = observers.iterator(); iterator.hasNext();) {
            Observer observer = iterator.next();
            observer.update();
        }
    }
}
